using SqlSugar;

using FSM.IBll;
using FSM.IDal;
using FSM.Model;

using System; 
using System.Collections.Generic; 
using System.Text; 
using Util.Excel;
using Util.Extensions;

namespace FSM.Bll
{ 
    public class TbCompanyBll: ITbCompanyBll 
    { 
        #region 模块 
        private readonly ITbCompanyDal _dal;
        private readonly ISysCodeBll _codeBll;
        private readonly IExcelAssistant _excel; 
        public TbCompanyBll(ITbCompanyDal dal, IExcelAssistant excel, ISysCodeBll codeBll) 
        { 
            _dal = dal; 
            _excel = excel;
            _codeBll = codeBll;
        } 
        #endregion 
 
        #region 查询 
 
        /// <summary> 
        /// 根据键值查询单个信息 
        /// </summary> 
        /// <param name="company_no">键值</param> 
        /// <returns></returns> 
        public tb_company Get(string company_no) 
        { 
            return _dal.GetById(company_no); 
        } 
 
        /// <summary> 
        /// 分页查询批量信息 
        /// </summary> 
        /// <param name="company_no">公司编号</param> 
        /// <param name="company_name">公司名称</param> 
        /// <param name="page">页码</param> 
        /// <param name="page_size">每页显示的条数</param> 
        /// <returns></returns> 
        public Tuple<List<tb_company>, int> ListPagin(string company_no, string company_name, int page, int page_size) 
        { 
            ISugarQueryable<tb_company> queryable = _dal.CreateQueryable(); 
            queryable = queryable.WhereIF(!string.IsNullOrEmpty(company_no),p => p.company_no == company_no); 
            queryable = queryable.WhereIF(!string.IsNullOrEmpty(company_name),p => p.company_name == company_name); 
             
            PageModel pm = new PageModel 
            { 
                PageIndex = page, 
                PageSize = page_size 
            }; 
            Tuple<List<tb_company>, int> result = _dal.GetPageListAndTotal(queryable, pm); 
            return result; 
        } 
 
        /// <summary> 
        /// 分页查询批量信息 
        /// </summary> 
        /// <param name="company_no">公司编号</param> 
        /// <param name="company_name">公司名称</param> 
        /// <returns></returns> 
        public List<tb_company> List(string company_no, string company_name) 
        { 
            Expressionable<tb_company> queryable = Expressionable.Create<tb_company>(); 
            queryable = queryable.AndIF(!string.IsNullOrEmpty(company_no),p => p.company_no == company_no); 
            queryable = queryable.AndIF(!string.IsNullOrEmpty(company_name),p => p.company_name == company_name); 
             
            List<tb_company> result = _dal.GetList(queryable.ToExpression()); 
            return result; 
        } 

        /// <summary>
        /// 根据关键字查询公司信息
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <returns></returns>
        public List<tb_company> ListByKeywords(string keywords)
        {
            Expressionable<tb_company> queryable = Expressionable.Create<tb_company>();
            queryable = queryable.AndIF(!string.IsNullOrEmpty(keywords), p => p.company_no.Contains(keywords) || p.company_name.Contains(keywords));
            List<tb_company> result = _dal.GetList(queryable.ToExpression());
            return result;
        }
 
        #endregion 
 
        #region 提交 
 
        /// <summary> 
        /// 添加 
        /// </summary> 
        /// <param name="model">实体</param> 
        /// <returns></returns> 
        public bool Insert(tb_company model) 
        {
            model.company_no = _codeBll.GetNewCode("Company");
            model.create_date = DateTime.Now;
            model.key = model.create_date.Value.ToString().ToMD5String().Substring(1, 10);
            return _dal.Insert(model); 
        } 
 
        /// <summary> 
        /// 更新 
        /// </summary> 
        /// <param name="model">实体</param> 
        /// <returns></returns> 
        public bool Update(tb_company model) 
        { 
            model.edit_date = DateTime.Now; 
            return _dal.UpdateExcludeColumns(model, "creater", "create_date") == 1; 
        } 
 
        /// <summary> 
        ///  删除 
        /// </summary> 
        /// <param name="company_no_arr">键值</param> 
        /// <returns></returns> 
        public bool Delete(params string[] company_no_arr) 
        { 
            return _dal.Delete(company_no_arr); 
        } 
 
        /// <summary> 
        /// 获得要导出的数据的Excel流 
        /// </summary> 
        /// <param name="company_no">公司编号</param> 
        /// <param name="company_name">公司名称</param> 
        /// <returns></returns> 
        public byte[] Export(string company_no, string company_name) 
        { 
            List<tb_company> result = List(company_no, company_name); 
            return _excel.Collection2ExcelBytes(result, "Sheet1"); 
        }
        #endregion
    } 
} 
